package com.projectName.www.servlet;


import com.projectName.www.po.Dish;
import com.projectName.www.po.User;
import com.projectName.www.service.SlrService;
import com.projectName.www.service.SlrServiceImpl;
import com.projectName.www.service.UserService;
import com.projectName.www.service.UserServiceImpl;
import com.projectName.www.util.Beanutils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Map;

@WebServlet("/addDishServlet")
public class AddDishServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

//        0.设置字符集
        request.setCharacterEncoding("utf-8");
        //1.获取id
        Map<String, String[]> map = request.getParameterMap();

        //3.封装对象

        Dish dish=new Dish();

        try {
            Beanutils.populate(dish, map);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        SlrService service=new SlrServiceImpl();
        service.addDish(dish);


        //5.跳转到userListServlet
        response.sendRedirect(request.getContextPath()+"/dishListServlet");
//
//        1.设置编码
//        request.setCharacterEncoding("utf-8");
//        //2.获取参数
//        String dame = request.getParameter("Dame");
//        String dNum = request.getParameter("dNum");
//        String dSum = request.getParameter("dSum");
//        String slrid = request.getParameter("slrid");
//        String typeid = request.getParameter("typeid");
//        String dPrice = request.getParameter("dPrice");
//        //3.封装对象
////        String dame, Integer typeid, Timestamp dTime, BigDecimal dPrice, int dNum, int dSum, String dImg
//        Dish dish=new Dish(dame,Integer.parseInt(typeid),new Timestamp(System.currentTimeMillis()),new BigDecimal(dPrice),Integer.parseInt(dNum),Integer.parseInt(dSum),null,Integer.parseInt(slrid));
//        System.out.println("----------------------------");
//        System.out.println(dish);
        //4.调用Service保存

    }
//
//
//0.设置字符集
//        request.setCharacterEncoding("utf-8");
//        //1.获取id
//        Map<String, String[]> map = request.getParameterMap();
//
//        //3.封装对象
////        User user=new User();
//        User user = new User();
//
//        System.out.println();
//        try {
//            Beanutils.populate(user, map);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//
//
//        //4.调用一个service保存
//        UserService us = new UserServiceImpl();
//        us.insert(user);
//
//        //5、跳转到userListServlet，使用重定向
//        response.sendRedirect(request.getContextPath() + "/userListServlet");
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
